<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="运输能力" prop="transportCapacity">
        <el-select v-model="queryParams.transportCapacity" placeholder="请选择运输能力" clearable
          @keyup.enter.native="handleQuery">
          <el-option label="全部" value=""></el-option>
          <el-option v-for="dict in dict.type.bas_transport_capacity" :key="dict.value" :label="dict.label"
            :value="dict.label"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="承运商名称/联系人" prop="carrierName" label-width="130px">
        <el-input v-model="queryParams.carrierName" placeholder="请输入承运商名称/联系人" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['basic:carrier:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['basic:carrier:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['basic:carrier:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['basic:carrier:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="carrierList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" type="index" width="68" />
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="承运商名称" align="center" prop="carrierName" />
      <el-table-column label="所在城市" align="center" prop="city" />
      <el-table-column label="业务联系人" align="center" prop="businessContact" />
      <el-table-column label="联系电话" align="center" prop="contactTel" />
      <el-table-column label="传真" align="center" prop="fax" />
      <el-table-column label="运输能力" align="center" prop="transportCapacity" />
      <el-table-column label="国税号" align="center" prop="nationaltaxNumber" />
      <el-table-column label="开户银行" align="center" prop="bank" />
      <el-table-column label="开户账号" align="center" prop="accountNumber" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['basic:carrier:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['basic:carrier:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改承运商对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row :gutter="10" class="infoRow">
          <el-col :span="8">
            <el-form-item label="承运商名称" prop="carrierName">
              <el-input v-model="form.carrierName" placeholder="请输入承运商名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系电话" prop="contactTel">
              <el-input v-model="form.contactTel" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="详细地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入详细地址" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10" class="infoRow">
          <el-col :span="8">
            <el-form-item label="营业执照" prop="businessLicense">
              <el-input v-model="form.businessLicense" placeholder="请输入营业执照" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="国税号" prop="nationaltaxNumber">
              <el-input v-model="form.nationaltaxNumber" placeholder="请输入国税号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="公司网址" prop="companyWebsite">
              <el-input v-model="form.companyWebsite" placeholder="请输入公司网址" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10" class="infoRow">
          <el-col :span="8">
            <el-form-item label="所在城市" prop="city">
              <el-cascader v-model="form.city" filterable :options="citys"
                :props="{ label: 'label', value: 'label', children: 'children' }" placeholder="请选择所在城市"></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="法人代表" prop="legalRepresentative">
              <el-input v-model="form.legalRepresentative" placeholder="请输入法人代表" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="注册资金(w)" prop="registeredCapital">
              <el-input v-model="form.registeredCapital" placeholder="请输入注册资金" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10" class="infoRow">

          <el-col :span="8">
            <el-form-item label="业务联系人" prop="businessContact">
              <el-input v-model="form.businessContact" placeholder="请输入业务联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系人手机" prop="contactPhone">
              <el-input v-model="form.contactPhone" placeholder="请输入联系人手机" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="运输能力" prop="transportCapacity">
              <el-select v-model="form.transportCapacity" placeholder="请选择运输能力">
                <el-option v-for="dict in dict.type.bas_transport_capacity" :key="dict.value" :label="dict.label"
                  :value="dict.label"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10" class="infoRow">
          <el-col :span="8">
            <el-form-item label="负责人手机" prop="principalPhone">
              <el-input v-model="form.principalPhone" placeholder="请输入负责人手机" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="开户银行" prop="bank">
              <el-select v-model="form.bank" filterable placeholder="请选择开户银行">
                <el-option v-for="bank in banks" :key="bank.value" :label="bank.text" :value="bank.text"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="开户账号" prop="accountNumber">
              <el-input v-model="form.accountNumber" placeholder="请输入开户账号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10" class="infoRow">
          <el-col :span="8">
            <el-form-item label="传真" prop="fax">
              <el-input v-model="form.fax" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" :autosize="{ minRows: 5, maxRows: 6 }" maxlength="255" clearable show-word-limit
                v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCarrier, getCarrier, delCarrier, addCarrier, updateCarrier } from "@/api/basic/carrier";
import bank from "@/api/basic/bank";
import city from "@/api/basic/city"


export default {
  name: "Carrier",
  dicts: ['bas_transport_capacity'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      citys: [],

      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 承运商表格数据
      carrierList: [],
      banks: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carrierName: null,
        transportCapacity: ""
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        carrierName: [
          { required: true, message: "承运商名称不能为空", trigger: "blur" }
        ],
        registeredCapital: [
          { required: true, message: "注册资金不能为空", trigger: "blur" }
        ],
        businessContact: [
          { required: true, message: "业务联系人不能为空", trigger: "blur" }
        ],
        principalPhone: [
          { required: true, message: "负责人手机不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询承运商列表 */
    getList() {
      this.citys = city.city;
      this.banks = bank.list;
      this.loading = true;
      listCarrier(this.queryParams).then(response => {
        const promises = response.rows.map(item => {
          let firstCommaIndex = item.city.indexOf(',');
          let lastCommaIndex = item.city.lastIndexOf(',');
          let city = item.city.substring(firstCommaIndex + 1, lastCommaIndex);
          item.city = city;
        });
        Promise.all(promises).then(() => {
          this.carrierList = response.rows;
          this.total = response.total;
          this.loading = false;
        });

      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        carrierId: null,
        carrierName: null,
        contactTel: null,
        address: null,
        businessLicense: null,
        nationaltaxNumber: null,
        companyWebsite: null,
        city: null,
        fax: null,
        legalRepresentative: null,
        registeredCapital: null,
        bank: null,
        remark: null,
        businessContact: null,
        transportCapacity: null,
        principalPhone: null,
        contactPhone: null,
        accountNumber: null,
        createBy: null,
        createTime: null,
        editedBy: null,
        editedTime: null,
        status: null,
        isDeleted: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.carrierId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加承运商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const carrierId = row.carrierId || this.ids
      getCarrier(carrierId).then(response => {
        if (response.data.city != null && response.data.city != "") {
          response.data.city = response.data.city.split(",")
        }
        this.form = response.data;
        this.open = true;
        this.title = "修改承运商";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let cityString;
          if (Array.isArray(this.form.city)) {
            cityString = this.form.city.join(',')
          }

          if (this.form.carrierId != null) {
            if (Array.isArray(this.form.city)) {
              this.form.city = cityString
            }
            updateCarrier(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            if (Array.isArray(this.form.city)) {
              this.form.city = cityString
            }
            addCarrier(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const carrierIds = row.carrierId || this.ids;
      this.$modal.confirm('是否确认删除承运商编号为"' + carrierIds + '"的数据项？').then(function () {
        return delCarrier(carrierIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('basic/carrier/export', {
        ...this.queryParams
      }, `carrier_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.infoRow {
  margin-bottom: 10px;
}
</style>
